package logic

import (
	"context"
	"encoding/json"
	"fmt"
	"regi-sys/cache"
	"regi-sys/model"
	"time"

	"regi-sys/regirpc/internal/svc"
	"regi-sys/regirpc/regirpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegiListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegiListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegiListLogic {
	return &RegiListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 挂号列表
func (l *RegiListLogic) RegiList(in *regirpc.RegiListRequest) (*regirpc.RegiListResponse, error) {
	// todo: add your logic here and delete this line
	var list []*regirpc.RegiList
	key := fmt.Sprintf("list_%s", in)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, key).Result()
	if cacheData == "" {
		list = model.RegiList(model.DB, in)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, key, jsonData, time.Minute*30)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	return &regirpc.RegiListResponse{
		List: list,
	}, nil
}
